package whf.framework.workflow.web;

import whf.framework.entity.AbstractEntity;
import whf.framework.web.struts.WebData;
import whf.framework.web.struts.WebDispatchAction;
import whf.framework.web.struts.WebForward;
import whf.framework.workflow.entity.WorkflowDefinition;
import whf.framework.workflow.service.WorkflowDefinitionService;
import whf.framework.workflow.service.WorkflowDefinitionServiceImp;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2008-06-21
 */
public class WorkflowDefinitionAction extends WebDispatchAction {
	@Override
	public WebForward list(AbstractEntity webEntity, WebData data) throws Exception {
		long parentId = data.getLongParameter("parentId", 0);
		String queryString = "t.parent.id=" + parentId;
		if(parentId == 0)
			queryString = "t.parent is null";
		else {
			WorkflowDefinition parent = WorkflowDefinitionServiceImp.getWorkflowDefinitionService().findByPrimaryKey(parentId);
			data.setAttribute("parent", parent);
		}
		data.setAttribute("queryString", queryString);
		return super.list(webEntity, data);
	}

	public WebForward deploy(AbstractEntity webEntity, WebData data) throws Exception {
		long id = webEntity.getId();
		if(id > 0) {
			WorkflowDefinitionService service = WorkflowDefinitionServiceImp.getWorkflowDefinitionService();
			WorkflowDefinition definition =service.findByPrimaryKey(id);
			WorkflowDefinition newDefinition = service.deploy(definition);
			data.putData("parentId", newDefinition.getParent().getId());
		}
		return list(webEntity, data);
	}
	
	public WebForward changeCurrent(AbstractEntity webEntity, WebData data) throws Exception {
		long id = webEntity.getId();
		if(id > 0) {
			WorkflowDefinitionService service = WorkflowDefinitionServiceImp.getWorkflowDefinitionService();
			WorkflowDefinition definition =service.findByPrimaryKey(id);
			service.changeCurrent(definition);
			data.putData("parentId", definition.getParent() != null?definition.getParent().getId(): id);
		}
		return list(webEntity, data);
	}
} 